package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IEbbEmailDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.StringUtils;
import com.wsj.util.tool.Tool;
import com.wsj.web.constant.page.PageContent;
import com.wsj.web.form.admin.ebbmail.EbbEmailManageForm;
import com.wsj.web.vo.homeSite.EbbEmail;

public class EbbEmailDaoImpl extends HibernateDaoSupport implements IEbbEmailDao {

	public void delete(EbbEmail ebbEmail) throws DataAccessException
	{
       this.getHibernateTemplate().delete(ebbEmail);
	}

	public EbbEmail loadById(Integer ebbEmailId) throws DataAccessException 
	{
		return (EbbEmail)this.getHibernateTemplate().get(EbbEmail.class, ebbEmailId);
	}

	/**
	 * 查询条件：发送人类型、接收人类型、标题、接收人ID、发送人ID、
	 * 邮件类型（点对点发或群发）、从时间1、到时间2
	 * */
	@SuppressWarnings("unchecked")
	public List<EbbEmail> loadList(Short sendType, Short reciverType,
			String title, Integer reciverId, Integer senderId, String mailType,
			String fromDate, String toDate,Short haveRead) throws DataAccessException {
		List<EbbEmail> list = null;
		StringBuffer stringBuffer = new StringBuffer(" from EbbEmail as e where 1=1 ");
		 //  发送人类型
        if(sendType != null){
			stringBuffer.append(" and e.bmUserTypeBySendType.id = " + sendType);
		}
        // 接收人类型
		if(reciverType != null){
			stringBuffer.append(" and e.bmUserTypeByReciverType.id = " + reciverType);
		}
		//  邮件标题
		if(StringUtils.isNotNull(title)){
			stringBuffer.append(" and e.title like " + "%"+title+"%");
		}
		//  接收人 ID
		if(reciverId != null){
			stringBuffer.append(" and e.reciverId = " + reciverId);
		}
		//  发送人 ID
		if(senderId != null){
			stringBuffer.append(" and e.senderId = " + senderId);
		}
		//  邮件发送类型
		if(StringUtils.isNotNull(mailType)){
			stringBuffer.append(" and e.mailType = " + mailType);
		}
		//  是否阅读
		if(haveRead != null){
			stringBuffer.append(" and e.haveRead = " + haveRead);
		}
		//  时间段
		if(fromDate != null && toDate != null){
			stringBuffer.append(" and e.sendTime >= '"+fromDate.trim()+" 00:00:00' and e.sendTime <= '"+toDate.trim()+" 23:59:59'");
		}
		
		stringBuffer.append(" order by e.haveRead asc, e.sendTime desc ");
		
		list = this.getHibernateTemplate().find(stringBuffer.toString());
		if(StringUtils.isNotNull(list)){
			return list;
		}
		return null;
	}

	public PageResultSet loadPageResultSet(Short sendType, Short reciverType,
			String title, Integer reciverId, Integer senderId, String mailType,
			String fromDate, String toDate,Short haveRead,Integer page,Integer pageSize) throws DataAccessException {
		// 初始化SQL
		PageResultSet pageResultSet = new PageResultSet();
		PerfectPageResultSet pageResult = new PerfectPageResultSet();
		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);
		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer(" from EbbEmail as e where 1=1 ");

		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
		
        //  发送人类型
        if(sendType != null){
			stringBuffer.append(" and e.bmUserTypeBySendType.id = ? ");
			list.add(sendType);
		}
        // 接收人类型
		if(reciverType != null){
			stringBuffer.append(" and e.bmUserTypeByReciverType.id = ? ");
			list.add(reciverType);
		}
		//  邮件标题
		if(StringUtils.isNotNull(title)){
			stringBuffer.append(" and e.title like ? ");
			list.add("%"+title+"%");
		}
		//  接收人 ID
		if(reciverId != null){
			stringBuffer.append(" and e.reciverId = ? ");
			list.add(reciverId);
		}
		//  发送人 ID
		if(senderId != null){
			stringBuffer.append(" and e.senderId = ? ");
			list.add(senderId);
		}
		//  邮件发送类型
		if(StringUtils.isNotNull(mailType)){
			stringBuffer.append(" and e.mailType = ? ");
			list.add(mailType);
		}
		//  是否阅读
		if(haveRead != null){
			stringBuffer.append(" and e.haveRead = ? ");
			list.add(haveRead);
		}
		//  时间段
		if(StringUtils.isNotNull(fromDate) && StringUtils.isNotNull(toDate)){
				stringBuffer.append(" and e.sendTime >= '"+fromDate.trim()+" 00:00:00' and e.sendTime <= '"+toDate.trim()+" 23:59:59'");
		}
		
		stringBuffer.append(" order by e.sendTime desc ");
			
		String[] strParams = Tool.toArray(list);
		pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
			
		return pageResultSet;
	}

	public void saveOrUpdate(EbbEmail ebbEmail) throws DataAccessException 
	{
		this.getHibernateTemplate().saveOrUpdate(ebbEmail);
	}

	/*****
	 * 添加站内短信息
	 */
	public void addEbbEmailDao(EbbEmail ebbEmail) throws DataAccessException 
	{
		this.getHibernateTemplate().save(ebbEmail);
		
	}

	/*****
	 * 加载邮件列表
	 */
	public PageResultSet loadEbbEmailListDao(EbbEmailManageForm ebbEmailManageForm) throws DataAccessException 
	{
		// 初始化SQL
		PageResultSet pageResultSet = new PageResultSet();
		PerfectPageResultSet pageResult = new PerfectPageResultSet();
		// 获取Session
		Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);
		// 初始化SQL语句
		StringBuffer stringBuffer = new StringBuffer(" from EbbEmail as e where 1= ? ");
		

		Integer page=PageContent.pageValue;
		Integer pageSize=PageContent.pageSizeValue;
		
		if(ebbEmailManageForm.getPage()!=null)
		{
			page = ebbEmailManageForm.getPage();
		}
		
		if(ebbEmailManageForm.getPageSize()!=null)
		{
			pageSize  = ebbEmailManageForm.getPageSize();
		}
		// 设置每页显示得记录数和开始页
		pageResult.setPage(page, pageSize);
		
        // 参数
        List<Comparable> list = new ArrayList<Comparable>();
        list.add("1");
        
        if(ebbEmailManageForm.getReciverTypeId()!=null)
        {
        	stringBuffer.append(" and e.bmUserTypeByReciverType.id = ?");
        	list.add(ebbEmailManageForm.getReciverTypeId());
        }
        
        if(ebbEmailManageForm.getReciverId()!=null)
        {
        	stringBuffer.append(" and e.reciverId = ?");
        	list.add(ebbEmailManageForm.getReciverId());
        }
        
        if(ebbEmailManageForm.getSendTypeId()!=null)
        {
        	stringBuffer.append(" and e.bmUserTypeBySendType.id = ?");
        	list.add(ebbEmailManageForm.getSendTypeId());
        }
        
        if(ebbEmailManageForm.getSenderId()!=null)
        {
        	stringBuffer.append(" and e.senderId= ?");
        	list.add(ebbEmailManageForm.getSenderId());
        }
        
        if(ebbEmailManageForm.getTitle()!=null  && !ebbEmailManageForm.getTitle().equals(""))
        {
        	stringBuffer.append(" and e.title  like ?");
        	list.add("%"+ebbEmailManageForm.getTitle()+"%");
        }
        
        if(ebbEmailManageForm.getContent()!=null  && !ebbEmailManageForm.getContent().equals(""))
        {
        	stringBuffer.append(" and e.title  like ?");
        	list.add("%"+ebbEmailManageForm.getTitle()+"%");
        }
        
//        /发信时间
        if(ebbEmailManageForm.getSendTimeStr()!=null  && !ebbEmailManageForm.getSendTimeStr().equals("")
        		&& ebbEmailManageForm.getSendTimeEndStr()!=null  && !ebbEmailManageForm.getSendTimeEndStr().equals(""))
        {
        	stringBuffer.append(" and e.sendTime  >= ?");
        	stringBuffer.append(" and e.sendTime  <= ?");
        	
        	list.add(ebbEmailManageForm.getSendTimeStr());
        	list.add(ebbEmailManageForm.getSendTimeEndStr());
        }
        
        //邮件类型
        if(ebbEmailManageForm.getMailType()!=null  && !ebbEmailManageForm.getMailType().equals(""))
        {
        	stringBuffer.append(" and e.mailType  = ?");
        	list.add(ebbEmailManageForm.getMailType());
        }
        
        stringBuffer.append(" order by e.sendTime desc ");
		
		String[] strParams = Tool.toArray(list);
		pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
			
		return pageResultSet;
        
        
	}

}
